package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.GoodsMapper;
import org.linlinjava.litemall.db.models.Goods;
import org.linlinjava.litemall.db.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements GoodsService {

    /**
     * 获取热卖商品
     *
     * @param offset
     * @param limit
     * @return
     */
    public IPage<Goods> queryByHot(int offset, int limit) {
        Page<Goods> page = new Page<>(offset, limit);
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("is_hot", true);
        query.eq("is_on_sale", true);
        query.orderByDesc("add_time");
        return this.baseMapper.selectPage(page, query);
    }

    /**
     * 获取新品上市
     *
     * @param pageNo
     * @param limit
     * @return
     */
    public IPage<Goods> queryByNew(int pageNo, int limit) {
        Page<Goods> page = new Page<>(pageNo, limit);
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("is_new", true);
        query.eq("is_on_sale", true);
        query.orderByDesc("add_time");
        return this.baseMapper.selectPage(page, query);
    }

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param pageNo
     * @param limit
     * @return
     */
    public IPage<Goods> queryByCategory(List<Long> catList, int pageNo, int limit) {
        Page<Goods> page = new Page<>(pageNo, limit);
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("is_on_sale", true);
        query.in("category_id", catList);
        query.orderByDesc("add_time");
        return this.baseMapper.selectPage(page, query);
    }


    /**
     * 获取分类下的商品
     *
     * @param catId
     * @param offset
     * @param limit
     * @return
     */
    public IPage<Goods> queryByCategory(Integer catId, int offset, int limit) {
        Page<Goods> page = new Page<>(offset, limit);
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("is_on_sale", true);
        query.eq("category_id", catId);
        query.orderByDesc("add_time");
        return this.baseMapper.selectPage(page, query);
    }

    @Override
    public IPage<Goods> querySelective(Integer catId, Integer brandId, String keywords, Boolean isHot, Boolean isNew, Integer offset, Integer limit) {
        Page<Goods> page = new Page<>(offset, limit);
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq(!StringUtils.isEmpty(catId) && catId != 0, "category_id", catId);
        query.eq(!StringUtils.isEmpty(brandId), "brand_id", brandId);
        query.eq(!StringUtils.isEmpty(isNew), "is_new", isNew);
        query.eq(!StringUtils.isEmpty(isHot), "is_hot", isHot);
        query.like(!StringUtils.isEmpty(keywords), "keyword", keywords);
        query.eq("is_on_sale", true);

       return baseMapper.selectPage(page, query);
    }

    @Override
    public IPage<Goods> querySelective(Integer goodsId, String goodsSn, String name, Integer pageNo, Integer pageSize) {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq(goodsId != null, "id", goodsId);
        query.eq(!StringUtils.isEmpty(goodsSn), "goods_sn", goodsSn);
        query.like(!StringUtils.isEmpty(name), "name", name);

        Page<Goods> page = new Page<>(pageNo, pageSize);
        return baseMapper.selectPage(page, query);
    }


    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id
     * @return
     */
    public Goods findByIdVO(Integer id) {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("id", id).eq("is_on_sale", true);
        return baseMapper.selectOne(query);
    }


    /**
     * 获取所有在售物品总数
     *
     * @return
     */
    public Integer queryOnSale() {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("is_on_sale", true);
        return baseMapper.selectCount(query);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     *
     * @return
     */
    public int count() {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        return baseMapper.selectCount(query);
    }

    public List<Long> getCatIds(Integer brandId, String keywords, Boolean isHot, Boolean isNew) {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq(!StringUtils.isEmpty(brandId), "brand_id", brandId);
        query.eq(!StringUtils.isEmpty(isNew), "is_new", isNew);
        query.eq(!StringUtils.isEmpty(isHot), "is_hot", isHot);
        query.like(!StringUtils.isEmpty(keywords), "keyword", keywords);
        query.eq("is_on_sale", true);

        List<Goods> goodsList = baseMapper.selectList(query);
        List<Long> cats = new ArrayList<>();
        for (Goods goods : goodsList) {
            cats.add(goods.getCategoryId());
        }
        return cats;
    }

    public boolean checkExistByName(String name) {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("name", name);
        query.eq("is_on_sale", true);
        return baseMapper.selectCount(query) > 0;
    }

    public List<Goods> queryByIds(Integer[] ids) {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("is_on_sale", true);
        query.in("id", Arrays.asList(ids));
        return baseMapper.selectList(query);
    }
}
